package com.device.config;

import com.device.entity.Device;
import com.device.entity.Rent;
import com.device.entity.UserDevice;
import com.device.repository.DeviceRepository;
import com.device.repository.RentRepository;
import com.device.repository.UserDeviceRepository;
import com.device.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * 自定义线程池
 */
@Component
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    private final UserDeviceRepository userDeviceRepository;
    private final RentRepository rentRepository;
    private final DeviceRepository deviceRepository;

    private final AdminService adminService;

    @Autowired
    public ScheduleConfig(UserDeviceRepository userDeviceRepository, RentRepository rentRepository, DeviceRepository deviceRepository, AdminService adminService) {
        this.userDeviceRepository = userDeviceRepository;
        this.rentRepository = rentRepository;
        this.deviceRepository = deviceRepository;
        this.adminService = adminService;
    }


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(customScheduler());
    }

    @Bean(destroyMethod = "shutdown")
    public ExecutorService customScheduler() {
        return Executors.newScheduledThreadPool(20);
    }


    @Scheduled(fixedDelay = 24 * 60 * 60 * 1000) // 每天更新设备租赁信息
    public void updateDeviceInfo() {

        List<UserDevice> userDeviceList = getDeviceList();
        userDeviceList.forEach(userDevice -> {
            userDevice.setStatus(true);
            userDevice.setEndTime(null);
            userDevice.setRentId(null);
            userDevice.setRentUser(null);
            userDevice.setRentInstitute(null);
        });
        userDeviceRepository.saveAll(userDeviceList);

        List<Rent> rentList = getRentList();
        rentList.forEach(
                rent -> {
                    rent.setStatus("租赁已到期");
                    Device device = deviceRepository.findByDeviceName(rent.getDeviceName());
                    adminService.updateDeviceIdleCount(device,rent.getDevice_area(),-rent.getDevice_num());
                }
        );
        rentRepository.saveAll(rentList);

    }

    private List<UserDevice> getDeviceList() {

        return  userDeviceRepository.findAll().stream()
                .filter(userDevice -> !userDevice.getStatus() && userDevice.getEndTime().before(new Date()))
                .collect(Collectors.toList());

    }

    private List<Rent> getRentList() {

        return  rentRepository.findAll().stream()
                .filter(rent -> rent.getStatus().equals("审核通过") && rent.getEndTime().before(new Date()))
                .collect(Collectors.toList());

    }

}